const express = require('express');
const router = express.Router();
const {Article, Likes}  = require('../models')

/* GET users listing. */
router.post('/', async function(req, res, next) {

    try {
        const userId = req.user_id
        const {articleId} = req.body
        console.log(userId, articleId, 'search')
        const like = await Likes.findOne({
            where: {
                user_id: userId,
                article_id: articleId

            }
        })
        console.log(like, articleId,userId)
        if(!like) {
            await Likes.create({
                user_id: userId,
                article_id: articleId
            })
            res.status(201).json( {status: true, message: '点赞成功'}); 
        } else {
            await like.distory()
            res.status(201).json( {status: true, message: '取消成功'}); 

        }
    } catch (error) {
        res.status(500).json({
            status: false, 
            message: 'error',
            error: [error.message]
        }) 
    }
  
});

module.exports = router;
